package com.returnsoft.resource.service;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;

import com.returnsoft.resource.dto.AreaDto;
import com.returnsoft.resource.eao.AreaEao;
import com.returnsoft.resource.entity.Area;
import com.returnsoft.resource.exception.ConversionExcepcion;
import com.returnsoft.resource.exception.EaoExcepcion;
import com.returnsoft.resource.exception.RecruitmentException;
import com.returnsoft.resource.util.Conversion;

/**
 * Session Bean implementation class DepartmentBean
 */
@Stateless
@LocalBean
public class AreaBean implements AreaInterface {

	@EJB
	private AreaEao eao;
	@EJB
	private Conversion conv;

	@Override
	public List<AreaDto> findAreasParent() throws RecruitmentException {
		try {
			List<Area> areas = eao.findAreasParent();
			List<AreaDto> areasDto = new ArrayList<AreaDto>();
			for (Area area : areas) {
				AreaDto areaDto = conv.fromArea(area);
				areasDto.add(areaDto);
			}
			return areasDto;
		} catch (EaoExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (ConversionExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new RecruitmentException(e.getMessage());
		}
	}

	@Override
	public List<AreaDto> findAreasChild(Integer areaId)
			throws RecruitmentException {
		try {
			List<Area> areas = eao.findAreasChild(areaId);
			List<AreaDto> areasDto = new ArrayList<AreaDto>();
			for (Area area : areas) {
				AreaDto areaDto = conv.fromArea(area);
				areasDto.add(areaDto);
			}
			return areasDto;
		} catch (EaoExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (ConversionExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new RecruitmentException(e.getMessage());
		}
	}

}
